package com.example.appmanager.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.appmanager.Appconfig.Appconfig;
import com.example.appmanager.R;
import com.example.appmanager.entity.AppEntity;
import com.example.appmanager.ui.home.HomeFragment;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
/*
* home_fragment_install.xml
* */
public class HomeFragmentAdapter extends RecyclerView.Adapter<HomeFragmentAdapter.AppViewHolder> {
    private Context context;
    private List<AppEntity.RowsBean> appEntities;
    private int progress;
    public HomeFragmentAdapter(Context context, List<AppEntity.RowsBean> appEntities) {
        this.context = context;
        this.appEntities = appEntities;
    }

    public HomeFragmentAdapter() {

    }

    public void setData(Context context, List<AppEntity.RowsBean> appEntities,int progress) {
        this.context = context;
        this.appEntities = appEntities;
        this.progress = progress;
        notifyDataSetChanged();
    }
    @NonNull
    @Override
    public AppViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.home_fragmeng_install, parent, false);
        return new AppViewHolder(view);
    }
    @Override
    public void onBindViewHolder(@NonNull AppViewHolder holder, int position) {
        if (!(appEntities.get(position).getAccount()==null)) {//网络请求成功！
            Log.d("AppAdapter", String.valueOf(appEntities.size()));
            AppEntity.RowsBean appEntity = appEntities.get(position);
            Log.d("AppAdapter", appEntity.getAppname());
            holder.textName.setText(appEntity.getAppname());
            holder.textSize.setText(appEntity.getCsize());//APP大小
            holder.textTime.setText(appEntity.getCtime());//APP时间


            if (appEntity.getPicture() == null) {
                Glide.with(context).load(Appconfig.BASE_IMAG + "APP.png").into(holder.imageView);
            } else {
                Glide.with(context).load(Appconfig.BASE_IMAG + appEntity.getPicture()).into(holder.imageView);
            }
        } else {
            //Toast.makeText(context, "数据错误，请检查网络是否连接成功！", Toast.LENGTH_LONG).show();
            holder.tip.setVisibility(View.VISIBLE);//
            holder.constraintLayout.setVisibility(View.INVISIBLE);
        }
    }
    @Override
    public int getItemCount() {
        return appEntities == null ? 0 : appEntities.size();
    }
    public class AppViewHolder extends RecyclerView.ViewHolder {

        private ImageView imageView;//APP图标
        private TextView textName;//APP名称
        private TextView textSize;//APP大小
        private TextView textTime;//APP时间
        private ProgressBar progressBar ;//进度条
        private TextView tip;//当用户没有下载的时候就会显示提示信息
        private ConstraintLayout constraintLayout;//这个是下载软件的
        public AppViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.imageView_picture);//APP图标
            textName = itemView.findViewById(R.id.textView_name_app);//APP名称
            textSize = itemView.findViewById(R.id.textView_size_app);//APP大小
            textTime = itemView.findViewById(R.id.textView_time);//APP的上传时间
            progressBar = itemView.findViewById(R.id.progressBar2);//获取进度条
            constraintLayout = itemView.findViewById(R.id.home_show_install);//
            tip = itemView.findViewById(R.id.home_textView_install);//
             progressBar.setProgress(progress);//设置进度
            itemView.getContext().getApplicationContext();


            progressBar.setProgressDrawable(ContextCompat.getDrawable(context,R.drawable.progress_bar));//设置进度条
        }
    }
}
